//http://mesowest.utah.edu/data/mesowest.tbl.gz data/mesowest.tbl.gz

import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.zip.*;
import java.util.Scanner;

/**
 * This program connects to a Web server and downloads the specified URL
 * from it.  It uses the HTTP protocol directly.
 **/
public class HttpClient {
	static int BUFFER = 2048;
  public static void main(String[] args) {
	  try {
		  
		  
		  
		  
		  StringTokenizer st = new StringTokenizer(""," ");
		  
		  
      	String data1 = st.nextToken();
      	if(data1.equals(""))
		  {
			  
		  }
      	
      	
		  //readMesoWest(args);
		  
		  //unzip the contents
		  unZip (args[0]);

		  //read file
		  String outFilename = args[0] +".out";	
		  //String rtn =  readFromFileStream(outFilename);
		  
		  // big file read
		  byte[] out = read2array(outFilename);
		  String outStr = new String(out);
		  System.out.println("Read Contents from File");
		  //System.out.println(outStr);
		  
		  // mesowest.tbl.gz.out
		  readMesoMD(outStr);
		  System.out.println(outStr);
		  //String st = outStr;
		  // end
		  System.out.println("Finished");
		  
		  
		  
		  Scanner sc2 = null;
		    
		        sc2 = new Scanner(outStr);
		    while (sc2.hasNextLine()) {
		            Scanner s2 = new Scanner(sc2.nextLine());
		        boolean b;
		        while (b = s2.hasNext()) {
		            String s = s2.next();
		            System.out.println(s);
		        }
		    }
		  
		  
	  }
    catch (Exception e) {    // Report any errors that arise
      System.err.println(e);
      System.err.println("Usage: java HttpClient <URL> [<filename>]");
    }
  }
  
  public static void readMesoMD(String fileStr)
  {
	  // mesowest.tbl.gz.out
	  // tokenize
	  //BDGER   9999999 Badger Wash    CO US  3934 -10894  1529   8 ACTIVE
	  String[] array = tokenize(fileStr, "\n"); //split each file into lines
	  
	  String[][] mesoTblArray = convertLineToTokens(array);
	  
	  System.out.println(" readMesoMD " );
	  /*
	  //get address field(Badger Wash)
	  int start = 16; int end = 49;
	  String[] addressArray = extractFromLine(array, start, end);
	  //get remaining field (BDGER   9999999  CO US  3934 -10894  1529   8 ACTIVE)
	  String[] remainingArray = removeFromLine(array, start, end);
	  
	  String[][] mesoOut = new String[remainingArray.length][];
	  for (int i=0; i<remainingArray.length; i++)
	  {
		  //System.out.println( i + "-> " + array[i]);
		  //String[] aLineArray = tokenize(array[i], ""); 
		  mesoOut[i] = tokenize(remainingArray[i], "");  // each line into tokens
		  if ( mesoOut[i].length != 9)
			  System.out.println("in a Line with mismatch of column" + i );
	  }
	  */
	  
	  // call prasanna api with "String[][] mesoOut" and "String[] addressArray"
	  
  }

  public static String[][] convertLineToTokens(String[] array)
  {	  
	  String[][] retArray = new String[array.length][];
	  for (int i=0; i<array.length; i++)
	  {
		  String[] aRow = new String[10];
	  // col0 start
	  int start = 0; int end = 8;
	  aRow[0] =  array[i].substring(start, end);
	  
	  //col1 start
	  start = 8; end = 16;
	  aRow[1] = array[i].substring(start, end);	 
	  
	  // col2 start
	  start = 16; end = 49;
	  aRow[2] = array[i].substring(start, end);	 
	  
	  // col3 start
	  start = 49; end = 52;
	  aRow[3] = array[i].substring(start, end);	 
	  
	  // col4 start
	  start = 52; end = 56;
	  aRow[4] = array[i].substring(start, end);	 
	  
	  // col5 start
	  start = 56; end = 61;
	  aRow[5] = array[i].substring(start, end);	 
	  
	  // col6 start
	  start = 61; end = 69;
	  aRow[6] = array[i].substring(start, end);	 
	  
	  // col7 start
	  start = 69; end = 75;
	  aRow[7] = array[i].substring(start, end);	 
	  
	  // col8 start
	  start = 75; end = 78;
	  aRow[8] = array[i].substring(start, end);	 
	  
	// col9 start
	  start = 78; end = 85;
	  aRow[9] = array[i].substring(start, array[i].length());	 
		  	  
	  	  
	  
	  retArray[i] = aRow;
		  
	  }
	  return retArray;
	  
  }
  
  public static void readMesoWest(String[] args) {
	    try {
	      // Check the arguments
	      if ((args.length != 1) && (args.length != 2))
	        throw new IllegalArgumentException("Wrong number of arguments");
	      
	      // Get an output stream to write the URL contents to
	      OutputStream to_file;
	      if (args.length == 2) to_file = new FileOutputStream(args[1]);
	      else to_file = System.out;
	      
	      // Now use the URL class to parse the user-specified URL into
	      // its various parts: protocol, host, port, filename.  Check the protocol
	      URL url = new URL(args[0]);
	      String protocol = url.getProtocol();
	      if (!protocol.equals("http"))
	        throw new IllegalArgumentException("URL must use 'http:' protocol");
	      String host = url.getHost();
	      int port = url.getPort();
	      if (port == -1) port = 80;  // if no port, use the default HTTP port
	      String filename = url.getFile();
	      // Open a network socket connection to the specified host and port
	      Socket socket = new Socket(host, port);
	      // Get input and output streams for the socket
	      InputStream from_server = socket.getInputStream();
	      PrintWriter to_server = 
	        new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
	      
	      // Send the HTTP GET command to the Web server, specifying the file.
	      // This uses an old and very simple version of the HTTP protocol
	      to_server.println("GET " + filename);
	      to_server.flush();  // Send it right now!
	      
	      // Now read the server's response, and write it to the file
	      byte[] buffer = new byte[4096];
	      int bytes_read;
	      while((bytes_read = from_server.read(buffer)) != -1)
	        to_file.write(buffer, 0, bytes_read);
	      
	      // When the server closes the connection, we close our stuff
	      socket.close();
	      to_file.close();	      
	     
	 //    System.out.println(rtn);
	    }
	    catch (Exception e) {    // Report any errors that arise
	      System.err.println(e);
	      System.err.println("Usage: java HttpClient <URL> [<filename>]");
	    }
	  }
 
  public static void unZip (String inputFile) {
	  //inputFile = inputFile.substring(0, inputFile.length() - 3);
	  try{

		  String inFilename = inputFile;
		  System.out.println("Opening the gzip file........		.................. :  opened");

		  GZIPInputStream gzipInputStream = null;
		  FileInputStream fileInputStream = null;
		  gzipInputStream = new GZIPInputStream(new 		FileInputStream(inFilename));
		  System.out.println("Opening the output file..		........... : opened");

		  String outFilename = inFilename +".out";		  
		  OutputStream out = new FileOutputStream(outFilename);
		  System.out.println("Transferring bytes from the 	compressed file to the output file........:     Transfer successful");
		  
		  byte[] buf = new byte[1024];  //size can be 		changed according to programmer's need.
		  int len;
		  while ((len = gzipInputStream.read(buf)) != -1) {
			  out.write(buf, 0, len);			  		  
		  }
		  gzipInputStream.close();
		  out.close();
	  }
		  catch(IOException e){
		  System.out.println("Exception has been thrown" + e);
		  }
	 }
  
  public static String readFromFileStream(String fileName) 
  {
	String charSet = "UTF-8";
  	File f = null;
  	StringBuffer sb = new StringBuffer();
  	try 
  	{		
			String cvFileName = fileName;
		
			f = new File(cvFileName);			
				
				// Start reading
				BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f), charSet)); 
				
				String tmp = null;
				String retVal = ""; 	
				
				while ((tmp = br.readLine()) != null)
				{
					if (tmp.length() > 1)
					{
						retVal = retVal + tmp;
						//System.out.println("A Line is - " + tmp);
					}
				}
				br.close();// End of read			
				
			//	System.out.println("retVal - " + retVal);
			return retVal;	
			
	} catch(Exception e) {	  
		e.printStackTrace();         
	}
	return sb.toString();
  }	    

 
  private static String[] extractFromLine(String[] array, int start, int end)
  {
	  String[] retArray = new String[array.length];
	  for (int i=0; i<array.length; i++)
	  {
		  retArray[i] = array[i].substring(start, end);
	  }
	  return retArray;
  }
  
  private static String[] removeFromLine(String[] array, int start, int end)
  {
	  String[] retArray = new String[array.length];
	  for (int i=0; i<array.length; i++)
	  {
		  retArray[i] = array[i].substring(0, start) +  array[i].substring(end, array[i].length());
	  }
	  return retArray;
  }
  
  /**
   *  Reads a file storing intermediate data into an array.
   *  @param file the file to be read
   *  @return a file data
   */
   public static byte[] read2array(String file) throws Exception {
      InputStream in = null;
      byte[] out             = new byte[0];
      try{
         in = new BufferedInputStream(new FileInputStream(file));
         // the length of a buffer can vary
         int bufLen = 20000*1024;
         byte[] buf = new byte[bufLen];
         byte[] tmp = null;
         int len    = 0;
         while((len = in.read(buf,0,bufLen)) != -1){
            // extend array
            tmp = new byte[out.length + len];
            // copy data
            System.arraycopy(out,0,tmp,0,out.length);
            System.arraycopy(buf,0,tmp,out.length,len);
            out = tmp;
            tmp = null;          
         }
      }finally{
         // always close the stream
         if (in != null) try{ in.close();}catch (Exception e){}
      }
      return out;
   }

   public static String[] tokenize(String inStr, String sep) 
   {
	   String[] retStr = null;
	   Vector v = new Vector();
	   StringTokenizer st = null;
	   if (inStr != null)
	   {
		   if (sep.length() > 0 )
			   st = new StringTokenizer(inStr, sep); 
		   else
			   st = new StringTokenizer(inStr); 
		   while(st.hasMoreTokens()) { 
			   String aLine = st.nextToken(); 
			   //System.out.println(aLine + "\t" + aLine); 
			   v.add(aLine);
			   } 
	   }
	   String [] s = (String []) v.toArray(new String[v.size()]);
	   return s; 
 	
   }

}

